import java.io.BufferedReader;
import java.io.InputStreamReader;

import awareness.defaults.PairEvent;
import collaboration.*;
import events.*;

//test de un objeto que soporta eventos
class test_object extends ListeningObject {
	private int x;
	private int y;
	
	public test_object()
	{
		super();
		x = 0;
		y = 0;
	}
	
	public void changeXY(int xx, int yy)
	{
		x = xx;
		y = yy;
		
		PairEvent ev = new PairEvent();
		ev.set_x(x);
		ev.set_y(y);
		
		awareness.Subsystem.EVENT_HANDLER.push(ev);
	}
};

//test de una clase listener
class test_listener implements EventListener {

	public test_listener()
	{
		
	}
	
	public boolean check_event(Event evt)
	{
		if (evt.get_type() == PairEvent.PAIR_EVENT_TYPE)
		{
			return true;
		}
		else return false;
	}
	
	public void action(Object receiver, Event data)
	{	
		System.out.println("recibido: "+data.float_member_value("x")+", "+data.float_member_value("y"));
	}
	
	public String name()
	{
		return "test_listener";
	}
}

public class test_client {
	public static void main(String[] args)
	{	
		if (!awareness.Subsystem.setup_environment(args))
		{
			System.out.println("error al inicializar el sistema de colaboracion");
			System.exit(1);
		}
		
		//////////// TEST DE ACCESO A METAINFORMACION ///////////////////
		
		System.out.println("Aplicaciones:");
		MIApplication ls[] = awareness.Subsystem.get_applications();
		int len = ls.length;
		for (int i=0; i<len; i++)
		{
			System.out.println(ls[i].name);
		}
		
		System.out.println("Usuarios de la primera aplicacion:");
		MIUser lu[] = awareness.Subsystem.get_users(ls[0].name);
		len = lu.length;
		for (int i=0; i<len; i++)
		{
			System.out.println(lu[i].name);
		}
		
		awareness.Subsystem.setup_MIServiceConnector(ls[0].name, "User", "clave");
		
		System.out.println("Usuarios conectados:");
		lu = awareness.Subsystem.MI_SERVICE_CONNECTOR.connected_users();
		len = lu.length;
		for (int i=0; i<len; i++)
			System.out.println(lu[i].name);
		
		System.out.println("Haciendo login");
		MIConnection mi_conn = awareness.Subsystem.MI_SERVICE_CONNECTOR.login();
		
		System.out.println("es administridador: "+(awareness.Subsystem.MI_SERVICE_CONNECTOR.admin()?"true":"false"));
		System.out.println("Mensaje de error: "+mi_conn.error_message);
		
		if (mi_conn.error_message.equals(""))
		{
			MIUser user = awareness.Subsystem.MI_SERVICE_CONNECTOR.user_info(); 
			System.out.println("tiempo ultimo de actualizacion: "+user.last_time_updated);
			long t = System.currentTimeMillis();
			System.out.println("tiempo actual: "+t);
			System.out.println("falta "+(t-user.last_time_updated)+" tiempo para auto-desconexion\n");
			awareness.Subsystem.MI_SERVICE_CONNECTOR.disconnect_user();
		}
		
		String role = awareness.Subsystem.MI_SERVICE_CONNECTOR.connect_user();
		System.out.println("Conexion de usuario, rol asignado: "+ role);
		
		//////////  TEST DE OBJETO EMISOR/RECEPTOR DE EVENTOS //////////
		test_object dobj = new test_object();
		test_listener listener = new test_listener();
		dobj.addEventListener(listener);
		
		awareness.Subsystem.EVENT_HANDLER.register_object(dobj);
		
		System.out.println("\n\nPrueba de componente distribuido: \n");
		float x,y;
		String str;
		while(true){
			System.out.println("introduzca numero para enviar evento: ");
			BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
			
			try{
			str = in.readLine();
			x = Float.parseFloat(str);
			str = in.readLine();
			y = Float.parseFloat(str);
			
			dobj.changeXY((int)x, (int)y);
			}catch(Exception ex)
			{
				ex.printStackTrace();
				System.exit(1);
			}
			
		} //bucle infinito para evitar el fin del cliente
	}
}
